clear all
set more off
estimates clear
capture log close


import excel "/Users/nwilso01/Dropbox/Food Waste/DATA/Choices_Paper.xlsx", sheet("Sheet1") firstrow


gen duration=minutes(endDate-startDate)
format duration %9.2f

*** Removes tests of the experiment***
drop if duration<60
drop if duration>130

***Generates and id base on session and computer number***
gen id=SessionNum*1000+COMP

***We ran the NY experiment after the 12th session***

gen State=1 if id>12000
replace State=0 if State==.
label define states 0 "Alabama" 1 "New York"
label values State states

***Eggs 

destring Risk Probability Loss, replace


rename _CODE female
rename _TEXT_CODE age
rename BL degree
replace degree=. if degree==5
rename BP income

replace income=10000 if income==1
replace income=25000 if income==2
replace income=65000 if income==6
replace income=75000 if income==7
replace income=85000 if income==8
replace income=95000 if income==9
replace income=110000 if income==10
replace income=115000 if income==11
replace income=125000 if income==12
replace income=135000 if income==13
replace income=145000 if income==14
replace income=155000 if income==15
replace income=165000 if income==16
replace income=175000 if income==17
replace income=185000 if income==18
replace income=195000 if income==19
replace income=215000 if income==20
gen inc=income/1000
gen lninc=ln(income)
gen income33=1 if income<=85000
replace income33=0 if income33==.
gen income66=2 if income>85000 & income<=115000
replace income66=0 if income66==.
gen income100=3 if income>115000
replace income100=0 if income100==.

gen incomegroups=income33+income66+income100
replace incomegroups=. if income==21
label define incs 1 "Low Income" 2 "Middle Income" 3 "High Income" 
label values incomegroups incs
replace income=. if income==21

label define sex 0 "Male" 1 "Female"
label values female sex


rename BN race
replace race=. if race==7
label define races 2 "Black" 3 "Asian" 4 "Hispanic" 5 "White" 6 "Other"
label values race races
gen white=1 if race==5
replace white=0 if white==.
replace age=53 if age==1964

rename BR married
replace married=. if married==3
label define marry 1 "Single" 2 "Married/Partner" 
label values married marry

label define edu 1 "High school or less" 2 "Associate Degree" 3 "College" ///
4 "Graduate" 
label values degree edu

gen colleged=1 if degree>=3
replace colleged=0 if colleged==.

***Eggs***

rename CV eggslowbestyn
rename CX eggslowbestcount

rename CZ eggshighbestyn
rename DB eggshighbestcount

rename DH eggslowuseyn
rename DJ eggslowusecount 

rename DD eggshighuseyn 
rename DF eggshighusecount 

generate eggtreatBL =1 if eggslowbestyn !=.
replace eggtreatBL=0 if eggtreatBL==.

generate eggtreatBH =1 if eggshighbestyn !=.
replace eggtreatBH=0 if eggtreatBH==.

generate eggtreatUL =1 if eggslowuseyn !=.
replace eggtreatUL=0 if eggtreatUL==.

generate eggtreatUH =1 if eggshighuseyn !=.
replace eggtreatUH=0 if eggtreatUH==.

generate eggtreat=1*eggtreatBL+2*eggtreatBH+3*eggtreatUL+4*eggtreatUH

label define dateprice 1 "Best $3" 2 "Best $4" 3 "Use $3" 4 "Use $4"
label values eggtreat dateprice

gen eggdate=1 if eggtreat==1 | eggtreat==2
replace eggdate=2 if eggdate==.
label define datelabel 1 "Best" 2 "Use"
label values eggdate datelabel

gen eggprice=1 if eggtreat==1 | eggtreat==3
replace eggprice=2 if eggprice==.
label define price 1 "$3" 2 "$4"
label values eggprice price


replace eggslowbestyn=0 if eggslowbestyn==.
replace eggshighbestyn=0 if eggshighbestyn==.
replace eggshighuseyn=0 if eggshighuseyn==.
replace eggslowusey=0 if eggslowusey==.


gen eggYN=eggslowbestyn+eggshighbestyn+eggshighuseyn+eggslowuseyn
replace eggYN=0 if eggYN==2

replace eggslowbestcount=0 if eggslowbestcount==. 
replace eggshighbestcount=0 if eggshighbestcount==. 
replace eggshighusecount=0 if eggshighusecount==. 
replace eggslowusecount=0 if eggslowusecount==. 

gen eggcount= eggslowbestcount+eggshighbestcount+eggshighusecount+eggslowusecount
gen eggwaste= 12-eggcount

gen eggwastep=100*eggwaste/12


***Deli Turkey Meat***

rename DL meatlowbestyn
rename QID375_1 meatlowbestcount

rename DP meathighbestyn
rename QID373_1 meathighbestcount

rename DX meatlowuseyn
rename QID453_1 meatlowusecount 

rename DT meathighuseyn 
rename QID451_1 meathighusecount 

generate meatTreatBL =1 if meatlowbestyn !=.
replace meatTreatBL=0 if meatTreatBL==.

generate meatTreatBH =1 if meathighbestyn !=.
replace meatTreatBH=0 if meatTreatBH==.

generate meatTreatUL =1 if meatlowuseyn !=.
replace meatTreatUL=0 if meatTreatUL==.

generate meatTreatUH =1 if meathighuseyn !=.
replace meatTreatUH=0 if meatTreatUH==.

generate meattreat=1*meatTreatBL+2*meatTreatBH+3*meatTreatUL+4*meatTreatUH

label values meattreat dateprice

gen meatdate=1 if meattreat==1 | meattreat==2
replace meatdate=2 if meatdate==.
label values meatdate datelabel

gen meatprice=1 if meattreat==1 | meattreat==3
replace meatprice=2 if meatprice==.
label values meatprice price

replace meatlowbestyn=0 if meatlowbestyn==.
replace meathighbestyn=0 if meathighbestyn==.
replace meathighuseyn=0 if meathighuseyn==.
replace meatlowusey=0 if meatlowusey==.


gen meatYN=meatlowbestyn+meathighbestyn+meathighuseyn+meatlowuseyn
replace meatYN=0 if meatYN==2

replace meatlowbestcount=0 if meatlowbestcount==.
replace meathighbestcount=0 if meathighbestcount==. 
replace meathighusecount=0 if meathighusecount==. 
replace meatlowusecount=0 if meatlowusecount==. 

gen meatcount= meatlowbestcount+meathighbestcount+meathighusecount+meatlowusecount
replace meatcount=0 if meatYN==0

gen meatwaste=100-meatcount


***Spaghetti Sauce***

rename EB saucehighbestyn
rename QID387_1 saucehighbestcount

rename EN saucelowbestyn
rename QID385_1 saucelowbestcount

rename EF saucelowuseyn
rename QID383_1 saucelowusecount 

rename EJ saucehighuseyn 
rename QID381_1 saucehighusecount 

generate sauceTreatBL =1 if saucelowbestyn !=.
replace sauceTreatBL=0 if sauceTreatBL==.

generate sauceTreatBH =1 if saucehighbestyn !=.
replace sauceTreatBH=0 if sauceTreatBH==.

generate sauceTreatUL =1 if saucelowuseyn !=.
replace sauceTreatUL=0 if sauceTreatUL==.

generate sauceTreatUH =1 if saucehighuseyn !=.
replace sauceTreatUH=0 if sauceTreatUH==.

generate saucetreat=1*sauceTreatBL+2*sauceTreatBH+3*sauceTreatUL+4*sauceTreatUH

label values saucetreat dateprice

gen saucedate=1 if saucetreat==1 | saucetreat==2
replace saucedate=2 if saucedate==.
label values saucedate datelabel

gen sauceprice=1 if saucetreat==1 | saucetreat==3
replace sauceprice=2 if sauceprice==.
label values sauceprice price

replace saucelowbestyn=0 if saucelowbestyn==.
replace saucehighbestyn=0 if saucehighbestyn==.
replace saucehighuseyn=0 if saucehighuseyn==.
replace saucelowuseyn=0 if saucelowuseyn==.


gen sauceYN=saucelowbestyn+saucehighbestyn+saucehighuseyn+saucelowuseyn
replace sauceYN=0 if sauceYN==2

replace saucelowbestcount=0 if saucelowbestcount==.
replace saucehighbestcount=0 if saucehighbestcount==. 
replace saucehighusecount=0 if saucehighusecount==. 
replace saucelowusecount=0 if saucelowusecount==. 

gen saucecount= saucelowbestcount+saucehighbestcount+saucehighusecount+saucelowusecount
replace saucecount=0 if sauceYN==0

gen saucewaste=100-saucecount



rename Ambiguity Amb
rename Probability Prob



gen Risklow=1 if Risk<=0.48
replace Risklow=0 if Risklow==.
gen Riskmid=2 if Risk>0.48 & Risk<0.735
replace Riskmid=0 if Riskmid==.
gen Riskhi=3 if Risk>=0.735
replace Riskhi=0 if Riskhi==.

gen Riskgroups=Risklow+Riskmid+Riskhi
label define risky 1 "Low Risk" 2 "Middle Risk" 3 "High Risk" 
label values Riskgroups risky


gen RiskDum=1 if Risk>0
replace RiskDum=0 if RiskDum==.

gen LossDum=1 if Loss>1
replace LossDum=0 if LossDum==.

gen AmbDum=1 if Ambiguity>0
replace AmbDum=0 if AmbDum==.

gen ProbDum=1 if Prob>1
replace ProbDum=0 if ProbDum==.

***Reshaping the data***
rename eggYN waste_1
rename meatYN waste_2
rename sauceYN waste_3

rename eggdate date_1
rename meatdate date_2
rename saucedate date_3

rename eggprice price_1
rename meatprice price_2
rename sauceprice price_3

gen temp_1="Egg"
gen temp_2="Meat"
gen temp_3="Sauce"


egen PDS_1 = concat(temp_1 id), punct(" ")
egen PDS_2 = concat(temp_2 id), punct(" ")
egen PDS_3 = concat(temp_3 id), punct(" ")

drop temp_*


reshape long waste_ date_ price_ PDS_, i(id) j(Time)
egen ProductDateSize= ends(PDS_), head punct(" ") 
egen Prodgroup= ends(ProductDateSize), head punct(_)
egen float Product = group(Prodgroup)
label define prods 1 "Eggs" 2 "Deli Meat" 3 "Spaghetti Sauce"  
label values Product prods
drop PDS ProductDateSize PDS_
rename waste_ waste 
rename price_ price
rename date_ treat
rename Product product

xtset id product

*** runs

by treat price product, sort: sum waste
by product price treat, sort: sum waste

kwallis waste, by(treat)
kwallis waste if price==1, by(treat)
kwallis waste if price==2, by(treat)
kwallis waste if product==1, by(treat)
kwallis waste if product==2, by(treat)
kwallis waste if product==3, by(treat)


eststo, title("Choices Paper"): logistic waste ib2.treat##ib2.product i.price , cluster (id)
margins treat, by(product)
marginsplot
margins r.treat, by(product )
marginsplot

estout using Choices_Paper.xls, cells(b(star label(Coef.)fmt(3)) se(label(SE) fmt(3))) ///
	starlevels(* 0.10 ** 0.05 *** 0.010) legend label baselevels varlabels(_cons Constant) ///
	stats(p  N, labels (Wald test N))  replace
	eststo clear



